package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.FundPay;
import com.edward.model.ShallPay;


public class ShallPayManager extends HibernateDaoSupport {

	public void addShallPay(ShallPay shallPay) {
		
		getHibernateTemplate().save(shallPay);
	}
	
	public void updateShallPay(ShallPay shallPay) {
		
		getHibernateTemplate().update(shallPay);
	}
	
	public void deleteShallPay(int id){
		
		getHibernateTemplate().delete(getShallPay(id));
	}
	
	public void updateForbid(FundPay fundPay ,int id){
		ShallPay s = getShallPay(id);
		s.setDbank(s.getDbank() - fundPay.getDbank());
		s.setDcash(s.getDcash() - fundPay.getDcash());
		s.setRbank(s.getRbank() - fundPay.getRbank());
		s.setRcash(s.getRcash() - fundPay.getRcash());
		if((s.getDbank() + s.getDcash())<=0 ||(s.getRbank() + s.getRcash())<=0){
			s.setTag(1);
			fundPay.setSpId(id);
			getHibernateTemplate().save(fundPay);
		}
		getHibernateTemplate().update(s);
		getHibernateTemplate().save(fundPay);
	}
	
	public void updateEnable(int id){
		ShallPay s = getShallPay(id);
		s.setTag(0);
		getHibernateTemplate().update(s);
	}
	
	public ShallPay getShallPay(int id){
		ShallPay d =(ShallPay)getHibernateTemplate().get(ShallPay.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<ShallPay> listShallPay() {
		return getHibernateTemplate().find("from ShallPay");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		List <ShallPay>list = getHibernateTemplate().find("from ShallPay s where s.type = '" + type +"' and s.tag = 0" );
		
		ShallPay sp = new ShallPay();
		for(ShallPay s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShallPay>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,ShallPay shallPay) {
		
		List <ShallPay>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from ShallPay s where s.type = '" + shallPay.getType() + "' and s.tag = 0";
		if(!shallPay.getCompany().equals("")){
			sql += "and s.company = '" + shallPay.getCompany() + "'";
		}
		if(!shallPay.getProposer().equals("")){
			
			sql += "and s.proposer = '" + shallPay.getProposer() + "'";
		}
		if(shallPay.getDate() != null && shallPay.getEndDate() == null ){
			String d = sdf.format(shallPay.getDate());
			
			sql += "and  s.date >= '" + d + "'";
		}
		if(shallPay.getEndDate() != null && shallPay.getDate() == null ){
			String d = sdf.format(shallPay.getEndDate());
		
			sql += "and  s.date <= '" + d + "'";
		}
		if(shallPay.getEndDate() != null && shallPay.getDate() != null ){
			String d1 = sdf.format(shallPay.getDate());
			String d2 = sdf.format(shallPay.getEndDate());
			
			sql += "and s.date between '" + d1 + "' and '" +d2  +"'";
		}
		list = getHibernateTemplate().find(sql);
		ShallPay sp = new ShallPay();
		for(ShallPay s : list){
			sp.setDbank(sp.getDbank() + s.getDbank());
			sp.setDcash(sp.getDcash() + s.getDcash());
			sp.setRbank(sp.getRbank() + s.getRbank());
			sp.setRcash(sp.getRcash() + s.getRcash());
		}
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShallPay>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



